package com.ityy.ruiji.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ityy.ruiji.common.R;
import com.ityy.ruiji.dao.RuiJiDao;
import com.ityy.ruiji.entity.Employee;
import com.ityy.ruiji.entity.Page;
import com.ityy.ruiji.exception.NameExistsException;
import com.ityy.ruiji.service.RuiJiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RuiJiServiceImpl implements RuiJiService {

    @Autowired(required = false)
    private RuiJiDao ruiJiDao;


    /**
     * 用户登陆的方法
     *
     * @param employee 里面用户提交的封装username与password
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
        //1. 根据用户名查找用户
        Employee dbEmployee = ruiJiDao.findByUserName(employee);
        //2. 如果查找的用户是空代表用户名不存在
        if (dbEmployee == null) {
            return R.error("用户名不存在");
        }
        //3. 如果用户存在则对比密码
        String dbpassword = dbEmployee.getPassword();
        String empassword = employee.getPassword();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(empassword.getBytes());
        if (!dbpassword.equals(md5DigestAsHex)) {
            return R.error("密码输入错误");
        }
        //4.如果密码也是没问题，那么查看用户的状态是否被冻结。
        if (dbEmployee.getStatus() == 0) {
            return R.error("用户已被冻结");
        }
        //5. 返回登陆成功结果
        return R.success(dbEmployee);
    }

    @Override
    public R save(Employee employee) {
        Employee userName = ruiJiDao.findByUserName(employee);
        if (userName != null) {
            throw new NameExistsException("用户名" + userName.getUsername() + "已存在");
        }
            //补全密码，状态、创建时间、修改时间
            employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
            employee.setStatus(1);
            employee.setCreateTime(LocalDateTime.now());
            employee.setUpdateTime(LocalDateTime.now());
            int row = ruiJiDao.save(employee);
            if (row > 0) {
                //插入成功
                return R.success("添加成功");
            } else {
                //插入失败
                return R.error("添加失败");
            }
        }

    @Override
    public R<Page> findByName(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Employee> byName = ruiJiDao.findByName(name);
        PageInfo<Employee> pageInfo = new PageInfo<>(byName);
        Page<Employee> employeePage = new Page<>(pageInfo.getList(),pageInfo.getTotal());

        return R.success(employeePage);
    }

    @Override
    public R update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        int rows = ruiJiDao.update(employee);
        if(rows>0){
            return R.success("更新成功");
        }else{
            return R.error("更新失败");
        }

    }

    @Override
    public R<Employee> findById(Long id) {
        Employee byId = ruiJiDao.findById(id);
        return R.success(byId);
    }
}
